package cn.empcode.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ConcretePrototype implements ProtoType {

	private String attr; // 成员属性

	public String getAttr() {
		return attr;
	}

	public void setAttr(String attr) {
		this.attr = attr;
	}

	public ConcretePrototype clone() {
		ConcretePrototype prototype = new ConcretePrototype();
		prototype.setAttr(this.attr);

		return prototype;
	}

	// 浅克隆
	public WeeklyLog easyClone() {
		Object object = null;
		try {
			object = super.clone();
			return (WeeklyLog) object;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
			return null;
		}

	}

	// 深度克隆
	public ConcretePrototype deepClone() throws IOException, ClassNotFoundException {
		// 将对象写入流中
		ByteArrayOutputStream bao = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bao);
		oos.writeObject(this);

		// 将对象从流中取出
		ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
		ObjectInputStream ois = new ObjectInputStream(bis);
		return (ConcretePrototype) ois.readObject();
	}
}
